package eerbaar.v2.effects.internal;

import java.awt.Graphics;
import java.awt.Graphics2D;

import eerbaar.game.IGame;
import eerbaar.persona.IPersona;
import eerbaar.position.IPosition;
import eerbaar.v2.app.view.IView;
import eerbaar.v2.app.view.Standard;
import eerbaar.v2.app.view.View;
import eerbaar.v2.graphics.Shadow;
import eerbaar.v2.graphics.Text;
import eerbaar.v2.graphics.rect.Rectangle2;

public class PopupRect {

    private final Text text;
    private final Rectangle2 coordinates;

    public PopupRect(IGame game, String text, Graphics graphics) {
        this.text = new Text(text);
        int left = calcLeftPosition(View.getInstance(), game, graphics);
        int top = calcTopPosition(View.getInstance(), game);
        int width = calcTextWidgth(graphics) + 10;
        int height = 20;
        this.coordinates = new Rectangle2(left, top, width, height);
    }

    private int calcLeftPosition(IView view, IGame game, Graphics graphics) {
        int left = 0;
        IPersona pet = game.getPersona("pet");
        IPosition p = pet.getPosition();
        int canvasX = 0;
        int squareX = view.getSquareWidth();
        left = canvasX + (squareX * p.getX());

        int right = left + this.calcTextWidgth(graphics) + 25;
        int canvasRight = canvasX + view.getWidth();
        if (right > canvasRight) {
            left -= (right - canvasRight);
        }

        return left;
    }

    private Rectangle2 calcTextStart(Graphics graphics) {

        Rectangle2 center = this.getCenter();
        int left = center.getLeft();
        int top = center.getTop();
        int textWidth = this.calcTextWidgth(graphics);
        left -= (textWidth / 2);
        top += 5;

        return new Rectangle2(left, top, 0, 0);
    }

    private int calcTextWidgth(Graphics graphics) {
        return this.text.getDimension(graphics).width;
    }

    private int calcTopPosition(IView view, IGame game) {

        int top = 0;
        IPersona pet = game.getPersona("pet");
        IPosition p = pet.getPosition();
        int squareY = view.getSquareHeight();
        int canvasY = 0;
        top = canvasY + (squareY * p.getY());
        if (p.getY() > 0) {
            top -= squareY;
        } else {
            top += squareY;
        }
        return top;
    }

    public void display(Graphics2D canvas) {

        displayBox(canvas);
        displayText(canvas);
    }

    private void displayBox(Graphics2D canvas) {

        Shadow shadow = new Shadow(this.coordinates);
        shadow.drawShadow(canvas);

        int left = this.coordinates.getLeft();
        int top = this.coordinates.getTop();
        int width = this.coordinates.width;
        int height = this.coordinates.height;

        canvas.setColor(Standard.WHEAT);
        canvas.fillRoundRect(left, top, width, height, 6, 6);
        canvas.setColor(Standard.DARK_BLUE);
        canvas.drawRoundRect(left, top, width, height, 6, 6);
    }

    private void displayText(Graphics2D canvas) {

        canvas.setColor(Standard.BLACK);
        Rectangle2 center = calcTextStart(canvas);
        int left = center.getLeft();
        int top = center.getTop();
        canvas.drawString(this.text.getText(), left, top);
    }

    private Rectangle2 getCenter() {

        int left = (int) (this.coordinates.getLeft() + (this.coordinates
                .getWidth() / 2f));
        int top = (int) (this.coordinates.getTop() + (this.coordinates
                .getHeight() / 2f));
        return new Rectangle2(left, top, 0, 0);
    }

}
